package cn.edu.qfnu.oj.entity;

import com.google.common.base.Objects;
import org.hibernate.annotations.DynamicUpdate;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import java.io.Serializable;
import java.util.HashSet;
import java.util.Set;

@Entity
@DynamicUpdate
public class Teacher implements Serializable {

    private static final long serialVersionUID = -6516604135788449253L;
    private String teacherId; //老师工号
    private String password; //密码
    private String realName; //真实姓名
    private String email; //邮箱
    private String sex; //性别
    private Set<Problem> problems = new HashSet<>();  //老师与问题是一对多关系
    private Set<Contest> contests = new HashSet<>(); //老师与比赛是一对多关系
    private Set<Task> tasks = new HashSet<>(); // 老师与作业是一对多关系

    public Teacher() {}

    public Teacher(String teacherId, String password, String email) {
        this.teacherId = teacherId;
        this.password = password;
        this.email = email;
    }

    @Id
    @Column(name = "teacher_id", length = 12, unique = true)
    public String getTeacherId() {
        return teacherId;
    }

    public void setTeacherId(String teacherId) {
        this.teacherId = teacherId;
    }

    @Column(length = 16)
    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    @Column(name = "real_name", length = 16)
    public String getRealName() {
        return realName;
    }

    public void setRealName(String realName) {
        this.realName = realName;
    }

    @Column(length = 32, unique = true)
    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    @Column(length = 8)
    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Teacher teacher = (Teacher) o;
        return Objects.equal(teacherId, teacher.teacherId) &&
                Objects.equal(password, teacher.password) &&
                Objects.equal(realName, teacher.realName) &&
                Objects.equal(email, teacher.email) &&
                Objects.equal(sex, teacher.sex) &&
                Objects.equal(problems, teacher.problems) &&
                Objects.equal(contests, teacher.contests) &&
                Objects.equal(tasks, teacher.tasks);
    }

    @Override
    public int hashCode() {
        return Objects.hashCode(teacherId, password, realName, email, sex, problems, contests, tasks);
    }

    @OneToMany(mappedBy = "teacher") //老师与问题的一对多关系交给题目管理
    public Set<Problem> getProblems() {
        return problems;
    }

    public void setProblems(Set<Problem> problems) {
        this.problems = problems;
    }

    @OneToMany(mappedBy = "teacher") //老师与比赛的一对多关系交给比赛管理
    public Set<Contest> getContests() {
        return contests;
    }

    public void setContests(Set<Contest> contests) {
        this.contests = contests;
    }

    @OneToMany(mappedBy = "teacher") //老师与作业的一对多关系交给作业管理
    public Set<Task> getTasks() {
        return tasks;
    }

    public void setTasks(Set<Task> tasks) {
        this.tasks = tasks;
    }
}
